/*
 * @Description:
 * @Version: 2.0
 * @Autor: wiz
 * @Date: 2023-07-18 09:13:33
 * @LastEditors: swc
 * @LastEditTime: 2024-02-01 17:51:59
 */
import { N_oMagicbox } from '@/types';
import { Tsrv, ifs, ts, tvs } from '../config';
import { N_cpms } from '../types';

export class T_pmsBase {
  // 当前帐套
  public accSetID: string;

  // 主组件箱
  protected compBox: ifs.I_compBox;

  // 主组件ID
  protected compID: string;

  // 主组件实例
  protected self: N_cpms.cPms;

  // 当前roleID
  protected _currRoleID: string = null;

  // 当前 roleFunID
  protected _currRoleFunsID: string = null;

  // 功能ID
  protected _currFunID: string = null;

  protected delFunsIDs: Array<string> = [];

  protected addFunIDs: Array<string> = [];

  // 浏览权限值
  protected _browseSwitchVal: boolean = false;

  public get currRoleID(): string {
    return this._currRoleID;
  }

  public set currRoleID(p: string) {
    this._currRoleID = p;
  }

  public get currRoleFunsID(): string {
    return this._currRoleFunsID;
  }

  public set currRoleFunsID(p: string) {
    this._currRoleFunsID = p;
  }

  public get browseSwitchVal(): boolean {
    return this._browseSwitchVal;
  }

  public set browseSwitchVal(p: boolean) {
    this._browseSwitchVal = p;
    if (p) {
      for (const obj of this.self.baseList) {
        if (obj.name === 'browse') {
          obj.val = p;
          break;
        }
      }
      this.setActs(null, false);
    } else {
      this.setActs(false, true);
    }
  }

  /**
   * @description: 设置所有允许浏览权限除外
   * @param {boolean} val 开关值
   * @param {boolean} disabled 禁用
   * @return {void}
   * @author: wiz
   * @Date: 2023-07-06 10:31:09
   * @LastEditors: wiz
   * @LastEditTime: Do not edit
   */
  public setActs(val: boolean = null, disabled: boolean = undefined) {
    this.self.fieldList.forEach(el => {
      val !== null && (el.val = val);
      disabled !== undefined && (el.disabled = disabled);
    });
    this.self.otherList.forEach(el => {
      val !== null && (el.val = val);
      disabled !== undefined && (el.disabled = disabled);
    });
    this.self.baseList.forEach(el => {
      val !== null && (el.val = val);
    });
  }

  public get currFunID(): string {
    return this._currFunID;
  }

  public set currFunID(p: string) {
    this._currFunID = p;
  }

  protected getMagicBoxConf(e: any): N_oMagicbox.I_conf {
    const filter = new ts.T_filter();
    // 复制时不包含本角色
    filter.filter = filter.parseNE('id', this.currRoleID);
    filter.mergeFilter({
      obj: filter.parseEQ(Tsrv.globalVar.fieldName.accountSetID, this.accSetID),
      s: tvs.V_filterLogical.AND
    });
    return {
      source: {
        limit: 1,
        field: Tsrv.globalVar.funName.PMS_ROLE,
        filter: {
          initSearch: filter
        }
      },
      config: {
        isMini: true,
        isDropdown: false,
        isFunLayout: false,
        miniBound: {
          x: e.clientX,
          y: e.clientY - 200,
          width: 200,
          height: 400
        },
        compSet: [
          {
            name: 'baseGrid',
            modify: [
              {
                // 设置config
                path: 'config.tableDataSetting.editable',
                value: false
              }
            ],
            // 通过config 传多选设定
            append: [{ key: 'multipleSelection', value: false }]
          }
        ]
      }
    };
  }

  protected openWindow(
    funName: string,
    e: any,
    config: N_oMagicbox.I_conf
  ): Promise<any> {
    return new Promise((resole, reject) => {
      Tsrv.utils
        .createModal('oMagicBox', config)
        .then(data => {
          resole({ data, e });
        })
        .catch(r => {
          reject(r);
        });
    });
  }

  protected findCanUseFuns(
    s: 'addFun' | 'addFunByFunGroup',
    funGroupID?: string
  ): Promise<[any, string]> {
    const params = {
      cmd: s,
      dbType: Tsrv.globalVar.dbType.MYSQL,
      lang: this.self.$i18n.locale,
      toRoleID: this.currRoleID,
      accSetID: this.accSetID
    };
    if (s === 'addFunByFunGroup') {
      if (Tsrv.utils.noValue(funGroupID)) {
        throw new Error('funGroupID 不能为空');
      }
      params['funGroupID'] = funGroupID;
    }
    return new Promise((resolve, reject) => {
      Tsrv.standardPostReq(params, Tsrv.lexicon.url.pmsHandle)
        .then(r => {
          resolve(r);
        })
        .catch(err => {
          reject(err);
        });
    });
  }

  public setAllFuns(): Promise<boolean> {
    const params = {
      cmd: 'cpAllFunByAccSet',
      dbType: Tsrv.globalVar.dbType.MYSQL,
      lang: this.self.$i18n.locale,
      toRoleID: this.currRoleID,
      accSetID: this.accSetID
    };

    return new Promise((resolve, reject) => {
      Tsrv.standardPostReq(params, Tsrv.lexicon.url.pmsHandle)
        .then(([r, m]) => {
          if (m || !r.success) {
            resolve(false);
          }
          resolve(true);
        })
        .catch(err => {
          reject(err);
        });
    });
  }

  protected funsSave(): Promise<[any, string]> {
    const params = {
      cmd: 'funChange',
      dbType: Tsrv.globalVar.dbType.MYSQL,
      lang: this.self.$i18n.locale,
      toRoleID: this.currRoleID,
      accSetID: this.accSetID,
      delRoleFunsIDs: this.delFunsIDs,
      addFunIDs: this.addFunIDs
    };
    return new Promise((resolve, reject) => {
      Tsrv.standardPostReq(params, Tsrv.lexicon.url.pmsHandle)
        .then(r => {
          this.delFunsIDs = [];
          this.addFunIDs = [];
          resolve(r);
        })
        .catch(err => {
          this.delFunsIDs = [];
          this.addFunIDs = [];
          reject(err);
        });
    });
  }
}

export interface I_pmsBase extends T_pmsBase {}
